package cn.edu.hit.education.dialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import cn.edu.hit.education.listener.DoubleDigitVerifyListener;
import cn.edu.hit.education.pojo.Course;
import cn.edu.hit.education.pojo.Schedule;
import cn.edu.hit.education.service.IScheduleService;
import cn.edu.hit.education.view.CourseScheduleView;
import educationschedule.Application;

import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.events.VerifyEvent;

/**
*@Title: separateCourse
*@Description:分散授课设置
*@author:李朝辉
*@data:2021年5月5日下午2:00
*
*/
public class SeparateCourseDialog extends TitleAreaDialog {

	CourseScheduleView courseScheduleview = Application.applicationContext.getBean(CourseScheduleView.class);
	private Text textCredict;
	private Text textPeriod;
	private Text textTheortical;
	private Text textExperiment;
	private int courseId;
	private IScheduleService scheduleService= Application.applicationContext.getBean(IScheduleService.class);
    private int semesterId = courseScheduleview.getSemester().getId();
	private int specialityId = 1;//courseScheduleview.getSpecialty().getId();
	
	private Course course;
	private Schedule schedule;
	
	public SeparateCourseDialog(Shell parentShell,int courseId) {
		super(parentShell);
		this.courseId = courseId;
	}
	
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite)super.createDialogArea(parent);
		createContentPane(composite);
		return composite;
	}
	
	private void createContentPane(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		composite.setLayout(new GridLayout(4, false));
		//gd_composite.widthHint = 174;
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		new Label(composite, SWT.NONE);
		
		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("本学期课程学分：");
		
		textCredict = new Text(composite, SWT.BORDER);
		textCredict.addVerifyListener(new DoubleDigitVerifyListener());
		textCredict.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		
		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_1.setText("本学期课程学时：");
		
		textPeriod = new Text(composite, SWT.BORDER);
		textPeriod.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label labelPeriod = new Label(composite, SWT.NONE);
		labelPeriod.setText("      ");
		new Label(composite, SWT.NONE);
		
		Label lblNewLabel_2 = new Label(composite, SWT.NONE);
		lblNewLabel_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_2.setText("本学期课程理论学时：");
		
		textTheortical = new Text(composite, SWT.BORDER);
		textTheortical.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label labelTheortical = new Label(composite, SWT.NONE);
		labelTheortical.setText("      ");
		new Label(composite, SWT.NONE);
		
		Label lblNewLabel_3 = new Label(composite, SWT.NONE);
		lblNewLabel_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_3.setText("本学期课程实验学时：");
		
		textExperiment = new Text(composite, SWT.BORDER);
		textExperiment.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label labelExperiment = new Label(composite, SWT.NONE);
		labelExperiment.setText("      ");
		
		this.setTitle("请输入本学期该课程授课：");
		this.setMessage("学分，学时，理论学时，实验学时",IMessageProvider.INFORMATION);
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("分散课程设置");
	}
	
	protected void createButtonsForButtonBar(Composite parent) {
        createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
        createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
    }
	
	protected Point getInitialSize() {
		// TODO 自动生成的方法存根
		return new Point(300,300);
	}
	
	protected void okPressed() {
	    // implement your own function here
		Schedule schedule = new Schedule();
		schedule.setCourseId(courseId);
		schedule.setSpecialtyId(specialityId);
		schedule.setSemesterId(semesterId);
		schedule.setPeriod(getPeriod());
		schedule.setCredits(getCredit());
		schedule.setTheoretical(getTheoretical());
		schedule.setExperiment(getExperiment());
		schedule.setOpened(1);
	    scheduleService.insert(schedule);
	     
	    super.okPressed();
	}
	
	
	public double getCredit() {
		return Double.parseDouble(textCredict.getText());
	}
	public double getPeriod() {
		return Double.parseDouble(textPeriod.getText());
	}
	public double getTheoretical() {
		return Double.parseDouble(textTheortical.getText());
	}
	public double getExperiment() {
		return Double.parseDouble(textExperiment.getText());
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public Schedule getSchedule() {
		return schedule;
	}

	public void setSchedule(Schedule schedule) {
		this.schedule = schedule;
	}
	
}
